package com.jinke.util;

import com.jinke.game.Explosion;

import java.awt.*;
import java.util.ArrayList;
import java.util.List;

public class ExplosionPool {
    public static final ExplosionPool instance = new ExplosionPool();

    private final List<Explosion> pool = new ArrayList<>();
    private final int POOL_SIZE = 20; // 对象池大小

    private ExplosionPool() {
        // 预先创建对象池
        for (int i = 0; i < POOL_SIZE; i++) {
            pool.add(new Explosion());
        }
    }

    public Explosion getExplosion(int x, int y) {
        for (Explosion explosion : pool) {
            if (!explosion.isActive()) {
                explosion.init(x, y);
                return explosion;
            }
        }
        // 如果对象池满了，创建新对象（可选）
        Explosion newExplosion = new Explosion();
        newExplosion.init(x, y);
        pool.add(newExplosion);
        return newExplosion;
    }

    public void drawAll(Graphics g) {
        for (Explosion explosion : pool) {
            explosion.draw(g);
        }
    }

    public List<Explosion> getAllExplosions() {
        return pool;
    }
}
